package cn.jeeweb.web.modules.sys.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mvc.controller.BaseBeanController;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.query.annotation.PageableDefaults;
import cn.jeeweb.common.query.data.Condition;
import cn.jeeweb.common.query.data.PropertyPreFilterable;
import cn.jeeweb.common.query.data.Queryable;
import cn.jeeweb.common.query.utils.QueryableConvertUtils;
import cn.jeeweb.common.security.shiro.authz.annotation.RequiresPathPermission;
import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.sys.entity.*;
import cn.jeeweb.web.modules.sys.service.*;
import cn.jeeweb.web.utils.PageRequest;
import cn.xjzn.framework_Core.ModuleActionType;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ui/searchscheme")
@ViewPrefix("modules/UiSearchScheme")
@RequiresPathPermission("ui:packageinfo")
@Log(title = "ui_searchscheme")
public class UiSearchSchemeController extends BaseBeanController<UiSearchScheme> {
	@Autowired
	IUiSearchSchemeService uiSearchSchemeService;

	@PostMapping("add")
	@Log(logType = ModuleActionType.INSERT)
//	@RequiresMethodPermissions("add")
	public Response add(UiSearchScheme entity, BindingResult result,
						HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		this.checkError(entity,result);
		if(StringUtils.isNotBlank(entity.getId())){
			uiSearchSchemeService.insertOrUpdate(entity);
		}else{
			uiSearchSchemeService.insert(entity);
		}
		return Response.ok("保存成功");
	}

	@PostMapping("{id}/update")
	@Log(logType = ModuleActionType.UPDATE)
//	@RequiresMethodPermissions("update")
	public Response update(UiSearchScheme entity, BindingResult result,
						   HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		this.checkError(entity,result);
		uiSearchSchemeService.insertOrUpdate(entity);
		return Response.ok("更新成功");
	}

	@PostMapping("{id}/delete")
	@Log(logType = ModuleActionType.DELETE)
//	@RequiresMethodPermissions("delete")
	public Response delete(@PathVariable("id") String id) {
		uiSearchSchemeService.deleteById(id);
		return Response.ok("删除成功");
	}

	@RequestMapping(value = "getById", method = { RequestMethod.GET, RequestMethod.POST })
	@Log(logType = ModuleActionType.SELECT)
	public Response getById(String id){
		UiSearchScheme uiSearchScheme = uiSearchSchemeService.getById(id);
		return Response.ok("获取成功",uiSearchScheme);
	}

	/**
	 * @param uiSearchScheme
	 * @throws IOException
     */
	@RequestMapping(value = "getList", method = { RequestMethod.GET, RequestMethod.POST })
	@Log(logType = ModuleActionType.SELECT)
//	@RequiresMethodPermissions("list")
	public void ajaxList(UiSearchScheme uiSearchScheme) throws IOException {
		List<UiSearchScheme> lists = uiSearchSchemeService.getList(uiSearchScheme);
		FastJsonUtils.print(lists);
	}

	@RequestMapping(value = "ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
	@PageableDefaults(sort = "id=desc")
	@Log(logType = ModuleActionType.SELECT)
//	@RequiresMethodPermissions("list")
	public void ajaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request, HttpServletResponse response) throws IOException {
		EntityWrapper<UiSearchScheme> entityWrapper = new EntityWrapper<>(entityClass);
		propertyPreFilterable.addQueryProperty(new String[]{"id","title","localKey","code","conditions","isDefault","moduleId","ownerId"});
		// 预处理
		QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
		SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
		// 预处理
		Page pageBean = uiSearchSchemeService.selectPage(PageRequest.getPage(),entityWrapper);
		FastJsonUtils.print(pageBean);
	}

	@RequestMapping(value = "getOperators", method = { RequestMethod.GET, RequestMethod.POST })
	@Log(logType = ModuleActionType.SELECT)
	public void getOperators(){
		List<Operator> list = new ArrayList<Operator>();
		Condition.Operator[] o = Condition.Operator.values();
		for(int i = 0; i < o.length; i ++){
			Operator operator = new Operator();
			String info = o[i].getInfo();
			String symbol = o[i].getSymbol();
			String name = o[i].name();
			operator.setInfo(info);
			operator.setSymbol(symbol);
			operator.setName(name);
			list.add(operator);
		}
		FastJsonUtils.print(list);
	}

}